package cn.itcast.producer.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * Copyright (C), 2022-2022, 姚兵
 * Author: 32210
 * Date: 2022/10/7 19:36
 * FileName: RabbitConfig
 * Description:
 */

@Configuration
public class RabbitConfig {

    public static final String deadRoutingKey="dead.routing.key";
    public static final String deadExchange="dead.routing.exchange";

    public static final String deadQueue="dead.queue";

    @Configuration
    public class QueueConfig{

        @Bean
        public Queue demo1(){
            return new Queue("simple.queue",true);
        }


        @Bean
        public Queue delayQueue(){

//            Map<String, Object> args = new HashMap<>(3);
//            // x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
//            args.put("x-dead-letter-exchange", deadExchange);
//            // x-dead-letter-routing-key  这里声明当前队列的死信路由key
//            args.put("x-dead-letter-routing-key", deadRoutingKey);
//            // x-message-ttl  声明队列的TTL
//            args.put("x-message-ttl", 6000);
//            Queue queue = new Queue("simple.delay", true, false, false,args);
//            return queue;
            return QueueBuilder.durable("simple.delay").deadLetterRoutingKey(deadRoutingKey).deadLetterExchange(deadExchange).ttl(6000).build();
        }


        @Bean
        Queue deadQueue(){
            return new Queue(deadQueue,true,false,false);
        }
    }


    @Configuration
    public class ExchangeConfig{
        @Bean
        public TopicExchange simpleExchange(){
            // 三个参数：交换机名称、是否持久化、当没有queue与其绑定时是否自动删除
            return new TopicExchange("simple.direct", true, false);
        }


        @Bean
        public DirectExchange delayExchange(){
            return new DirectExchange("simple.delay",true,true);
        }


        @Bean
        public DirectExchange deadExchange(){
            return new DirectExchange(deadExchange,true,true);
        }



    }


    @Configuration
    public class BindingConfig{
//        @Autowired
//        Queue demo1;
//
//        @Autowired
//        TopicExchange simpleExchange;



        @Bean
        Binding demoBinding(Queue demo1,TopicExchange simpleExchange){
            return   BindingBuilder.bind(demo1).to(simpleExchange).with("simple.#");
        }

        @Bean
        Binding delayBinding(Queue delayQueue,DirectExchange delayExchange){
            return BindingBuilder.bind(delayQueue).to(delayExchange).with("simple.delay");
        }

        @Bean
        Binding deadBinding(Queue deadQueue,DirectExchange deadExchange){
            return BindingBuilder.bind(deadQueue).to(deadExchange).with(deadRoutingKey);
        }
    }


    @Bean
    public RabbitAdmin amqpAdmin(CachingConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);
    }

    @Bean
    public RabbitTemplate rabbitTemplate(CachingConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(jsonMessageConverter());
        rabbitTemplate.setConfirmCallback((CorrelationData correlationData, boolean b, String s)->{
            System.out.println(correlationData);
            System.out.println(b);
            System.out.println(s);
            System.out.println("confirm callback");
        });

        rabbitTemplate.setReturnCallback((Message message, int i, String s, String s1, String s2)->{
            System.out.println(message);
            System.out.println("return callback");
        });
        return rabbitTemplate;
    }

    @Bean
    public Jackson2JsonMessageConverter jsonMessageConverter()
    {
        return new Jackson2JsonMessageConverter();
    }


    //设置接收方转换json
    @Bean
    public SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        //设置json序列化 消息转换器
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        return factory;
    }

}
